package cn.yly.esearch.sql4es.http;

//import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

import com.gbasedbt.base64.BASE64Encoder;

import cn.yly.esearch.sql4es.EsTool;


public class EsConfig {
    private String jdbcUrl;
    private String url;
    private String noIndexUrl;
    private String username;
    private String password;
    private String auth;
    private String index;
    private int connectTimeout;
    private int readTimeout;


    public EsConfig(String jdbcUrl, Properties props,String index) {
    	 this.index=index;
    	 init(jdbcUrl, props);
    }

    private void init(String jdbcUrl, Properties props) {
        this.jdbcUrl = jdbcUrl;
        noIndexUrl= (jdbcUrl.contains("http") ? "" : "http://")
                + (jdbcUrl.replaceFirst(EsTool.URL_PREFIX, "") + "/").replace("//", "/");
        this.url = noIndexUrl  + index ;
        // BASE64加密
        this.username = props.getProperty("user", props.getProperty("username"));
        this.password = props.getProperty("pass", props.getProperty("password"));
        this.auth = "Basic " + new BASE64Encoder().encode((username + ":" + password).getBytes(StandardCharsets.UTF_8));

        this.connectTimeout = EsTool.parseInt(props.getProperty("connectTimeout"), 30000);
        this.readTimeout = EsTool.parseInt(props.getProperty("readTimeout"), 60000);
    }

    
    public String getNoIndexUrl() {
		return noIndexUrl;
	}

	public void setNoIndexUrl(String noIndexUrl) {
		this.noIndexUrl = noIndexUrl;
	}

	public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAuth() {
        return auth;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}
    
}
